package com.example.wechat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.Window;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Fragment mTab01 = new WeixinFragment();
    private Fragment mTab02 = new FriendFragment();
    private Fragment mTab03 = new ContactFragment();
    private Fragment mTab04 = new SettingFragment();

    private FragmentManager fm;

    private LinearLayout mTabWeiXin;
    private LinearLayout mTabFriend;
    private LinearLayout mTabContact;
    private LinearLayout mTabSetting;

    private ImageButton mImgWeiXin;
    private ImageButton mImgFriend;
    private ImageButton mImgContact;
    private ImageButton mImgSetting;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initFragment();
        initView();
        initEvent();
        SelectFragment(0);
    }

    private void initFragment(){
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.content,mTab01);
        transaction.add(R.id.content,mTab02);
        transaction.add(R.id.content,mTab03);
        transaction.add(R.id.content,mTab04);
        transaction.commit();
    }

    private void initEvent(){
        mTabWeiXin.setOnClickListener(this);
        mTabFriend.setOnClickListener(this);
        mTabContact.setOnClickListener(this);
        mTabSetting.setOnClickListener(this);
    }

    private void initView(){
        mTabWeiXin = (LinearLayout)findViewById(R.id.tab_weixin);
        mTabFriend = (LinearLayout)findViewById(R.id.tab_friend);
        mTabContact = (LinearLayout)findViewById(R.id.tab_contact);
        mTabSetting = (LinearLayout)findViewById(R.id.tab_setting);

        mImgWeiXin = (ImageButton)findViewById(R.id.weixin_img);
        mImgFriend = (ImageButton)findViewById(R.id.frd_img);
        mImgContact = (ImageButton)findViewById(R.id.contact_img);
        mImgSetting = (ImageButton)findViewById(R.id.setting_img);
    }

    private void HideFragment(FragmentTransaction transaction){
        transaction.hide(mTab01);
        transaction.hide(mTab02);
        transaction.hide(mTab03);
        transaction.hide(mTab04);
    }

    private void SelectFragment(int i){
        FragmentTransaction transaction= fm.beginTransaction();
        HideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(mTab01);
                mImgWeiXin.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                transaction.show(mTab02);
                mImgFriend.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                transaction.show(mTab03);
                mImgContact.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                transaction.show(mTab04);
                mImgSetting.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        ResetImg();
        switch (v.getId()){
            case R.id.tab_weixin:
                SelectFragment(0);
                break;
            case R.id.tab_friend:
                SelectFragment(1);
                break;
            case R.id.tab_contact:
                SelectFragment(2);
                break;
            case R.id.tab_setting:
                SelectFragment(3);
                break;
            default:
                break;
        }
    }

    public void ResetImg(){
        mImgWeiXin.setImageResource(R.drawable.tab_weixin_normal);
        mImgFriend.setImageResource(R.drawable.tab_find_frd_normal);
        mImgContact.setImageResource(R.drawable.tab_address_normal);
        mImgSetting.setImageResource(R.drawable.tab_settings_normal);

    }
}